<?php

namespace app\models;

use yii;
use app\components\BaseActiveRecord;
use yii\helpers\Json;

/**
 * This is the model class for table "{{%user_profile}}".
 *
 * @property integer $id
 * @property integer $uid
 * @property string $email
 * @property integer $sex
 * @property string $school
 * @property string $wechat
 * @property string $major
 * @property integer $school_time
 * @property string $company_code
 * @property string $experience1
 * @property string $experience2
 * @property string $experience3
 * @property string $experience4
 * @property string $experience5
 * @property string $style_images
 * @property string $identity_images
 * @property string $user_images
 * @property string $product_images
 * @property string $home_link
 * @property string $province
 * @property string $city
 * @property string $industry
 * @property string $address
 * @property string $company_scale
 *
 * @property User $user
 */
class UserProfile extends BaseActiveRecord {


    /**
     * @inheritdoc
     */
    public static function tableName() {
        return '{{%user_profile}}';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['school', 'sex', 'major', 'wechat', 'school_time', 'email', 'experience1'], 'required', 'on' => 'student'],
            [['identity_images', 'address', 'industry', 'company_scale', 'email', 'company_code', 'province', 'city', 'experience1', 'experience2', 'identity_images', 'style_images', 'user_images'], 'required', 'on' => 'company'],

            [['uid', 'sex'], 'integer'],
            [['email', 'company_code', 'province', 'city'], 'string', 'max' => 60],
            [['school', 'wechat', 'home_link', 'company_scale', 'major'], 'string', 'max' => 120],
            [['experience1', 'experience2', 'experience3', 'experience4', 'experience5'], 'string', 'max' => 1024],
            [['style_images', 'identity_images', 'user_images', 'product_images'], 'string', 'max' => 220],
            [['industry', 'address'], 'string', 'max' => 250]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'uid' => 'Uid',
            'email' => '邮箱',
            'sex' => '性别',
            'school' => '学校',
            'wechat' => '微信',
            'major' => '专业',
            'company_code' => '企业代码',
            'experience1' => $this->user->getIsStudent() ? '自我介绍' : '基本信息',
            'experience2' => '详细介绍',
            'experience3' => 'Experience3',
            'experience4' => 'Experience4',
            'experience5' => 'Experience5',
            'style_images' => '风貌展示',
            'identity_images' => '副本照片',
            'user_images' => 'User Images',
            'product_images' => '产品介绍',
            'home_link' => '官网链接',
            'province' => '省份',
            'city' => '城市',
            'industry' => '企业行业',
            'address' => '详细地址',
            'company_scale' => '企业规模',
        ];
    }

    public function getUser() {
        return $this->hasOne(User::className(), ['id' => 'uid']);
    }

    public function beforeValidate() {
        $fields = ['style_images', 'identity_images', 'user_images', 'product_images'];
        foreach ($fields as $field) {
            if (is_array($this->$field) && !empty($this->$field)) {
                $this->$field = Json::encode($this->$field);
            }
        }
        if ($this->school_time) {
            $this->school_time = strtotime($this->school_time);
        }

        return parent::beforeValidate();
    }

    public static function getModel($uid) {
        $model = UserProfile::findOne(['uid' => $uid]);
        if (!$model) {
            $model = new UserProfile();
            $model->uid = $uid;
            $model->save(FALSE);
        }
        return $model;
    }

    public function getThumbsByFiled($filed) {
        return empty($this->$filed) ? [] : Json::decode($this->$filed);
    }
}
